package com.digdes.calendar.gwt.client.widget;

import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.digdes.calendar.gwt.client.dto.EventParticipantDTOImpl;
import com.digdes.calendar.gwt.client.service.CalendarService;
import com.digdes.calendar.gwt.client.service.CalendarServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.List;

public class ShowEventDBox extends PopupPanel implements ClickListener {
    private EventDTOImpl eventDTO;
    private Button closeBtn = new Button("Закрыть");
    private Button editBtn = new Button("Изменить");
    public Button deleteBtn = new Button("Удалить");
    private Data data = new Data();
    private VerticalPanel vp = new VerticalPanel();
    private final CalendarServiceAsync calendarService = GWT
            .create(CalendarService.class);
    private DateUtil date = new DateUtil();

    public ShowEventDBox(EventDTOImpl eventDTO) {
        this.eventDTO = eventDTO;
        setWidget(vp);
        setSize("450px", "100%");
        closeBtn.addClickListener(this);
        deleteBtn.addClickListener(this);

        HorizontalPanel hp = new HorizontalPanel();
        hp.add(editBtn);
        hp.add(deleteBtn);
        hp.add(closeBtn);
        VerticalPanel vpData = new VerticalPanel();
        HorizontalPanel hpTitle = new HorizontalPanel();

        Label nameEvent = new Label(eventDTO.getNameEvent());
        nameEvent.setStyleName("nameEvent");
        hpTitle.setStyleName("hpTitle");
        hpTitle.add(nameEvent);

        String dateS = eventDTO.getDateStart().getDate() + " " +
                data.otherMonths[eventDTO.getDateStart().getMonth()] + ", " +
                data.daysFull[eventDTO.getDateStart().getDay()];
        Label dateEvent = new Label(dateS);
        String datePeriod = "c " + eventDTO.getDateStart().getDate() + " " +
                data.otherMonths[eventDTO.getDateStart().getMonth()] + " по " +
                +eventDTO.getDateFinish().getDate() + " " +
                data.otherMonths[eventDTO.getDateFinish().getMonth()];
        Label datePeriodLbl = new Label(datePeriod);

        String timeStart = eventDTO.getDateStart().getHours() + ":" +
                date.addZeroInMinutes(eventDTO.getDateStart().getMinutes());
        String timeFinish = eventDTO.getDateFinish().getHours() + ":" +
                date.addZeroInMinutes(eventDTO.getDateFinish().getMinutes());
        Label timeEvent = new Label("Время проведения: " + timeStart + " - " + timeFinish);
        if ((eventDTO.getDateStart().getDate() == eventDTO.getDateFinish().getDate()) &&
                (eventDTO.getDateStart().getMonth() == eventDTO.getDateFinish().getMonth())) {
            hpTitle.add(dateEvent);
            vpData.add(hpTitle);
            vpData.add(timeEvent);
        } else {
            hpTitle.add(datePeriodLbl);
            vpData.add(hpTitle);
        }

        HorizontalPanel hpPlace = new HorizontalPanel();
        Label placeLbl = new Label("Место проведения:");
        Label myPlaceEvent = new Label(eventDTO.getMyPlace());
        placeLbl.setStyleName("placeLblShow");
        hpPlace.add(placeLbl);
        if (eventDTO.getPlace() != null) {
            Label placeEvent = new Label(eventDTO.getPlace().getAddress());
            hpPlace.add(placeEvent);
        } else hpPlace.add(myPlaceEvent);
        vpData.add(hpPlace);

        HorizontalPanel hpAvtor = new HorizontalPanel();
        Label avtorLbl = new Label("Автор: " + eventDTO.getAuthor().getSurname() + " " +
                eventDTO.getAuthor().getName().charAt(0) + ". " +
                eventDTO.getAuthor().getPatronymic().charAt(0) + ". ");
        Label postLbl = new Label("(" + eventDTO.getAuthor().getPost() + ")");
        postLbl.setStyleName("postLbl");
        hpAvtor.add(avtorLbl);
        hpAvtor.add(postLbl);
        vpData.add(hpAvtor);

        String description = eventDTO.getDescription();
        String descrEmpty = "отсутствует";
        Label descrEvent = new Label();
        if (eventDTO.getDescription() != null) {
            descrEvent.setText("Описание: " + description);
        } else descrEvent.setText("Описание: " + descrEmpty);
        vpData.add(descrEvent);

        HorizontalPanel hpParticipants = new HorizontalPanel();
        Label participantsLabel  = new Label("Участники: ");
        ScrollPanel panelParticipants = new ScrollPanel();
        panelParticipants.setHeight("50px");
        panelParticipants.setWidth("100%");
        String participant = " ";
        String participantName;
        for (EventParticipantDTOImpl e : eventDTO.getParticipants()){
            participantName = e.getParticipant().getSurname() + " " +
                     e.getParticipant().getName().charAt(0) + ". " +
                e.getParticipant().getPatronymic().charAt(0) + ".";
                participant += " " + participantName + ",";
        }
        panelParticipants.add(new HTML(participant));
        hpParticipants.add(participantsLabel);
        hpParticipants.add(panelParticipants);
        vpData.add(hpParticipants);

       /* HorizontalPanel hpFiles = new HorizontalPanel();
        Label filesLabel  = new Label("Участники: ");
        ScrollPanel panelFiles = new ScrollPanel();
        panelFiles.setHeight("50px");
        panelFiles.setWidth("100%");
        String file = " ";
        String fileName;
        for (EventParticipantDTOImpl e : eventDTO.getParticipants()){
            participantName = e.getParticipant().getSurname() + " " +
                     e.getParticipant().getName().charAt(0) + ". " +
                e.getParticipant().getPatronymic().charAt(0) + ".";
                participant += " " + participantName + ",";
        }
        panelParticipants.add(new HTML(participant));
        hpParticipants.add(participantsLabel);
        hpParticipants.add(panelParticipants);
        vpData.add(hpParticipants);
        for(FileDTOImpl f : eventDTO.getFiles()){
            System.out.println(f.getFilepath());
        }*/
        vp.add(vpData);
        vp.add(hp);
        vpData.setStyleName("vpData");
        vp.setStyleName("showEvent");
        setAnimationEnabled(true);
        setAutoHideEnabled(true);
        setGlassEnabled(true);
    }

    public EventDTOImpl getEventDTO() {
        return eventDTO;
    }

    public void setEventDTO(EventDTOImpl eventDTO) {
        this.eventDTO = eventDTO;
    }

    public void onClick(Widget sender) {
        if (sender == closeBtn) {
            hide();
        }
        if (sender == deleteBtn) {
          //  deleteEvent();
        }
    }

    public void deleteEvent(EventLabel removingEvent,  List<EventLabel> eventsLabel) {
         for (EventLabel label : eventsLabel) {
            if (label.getEventDTO() == removingEvent.getEventDTO())
                label.removeFromParent();
        }
            hide();
        calendarService.deleteEvent(getEventDTO().getId(), new AsyncCallback<Void>() {
            public void onFailure(Throwable caught) {

                //Window.alert("Fail");
            }

            public void onSuccess(Void result) {
                //Window.alert("Succes");
            }
        });
    }
}
